﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgriMarketWebProj.AgriMarketService;
using AgriMarketWebProj.Models;
namespace AgriMarketWebProj.Controllers
{
    public class TypeOfAuctionAdminController : Controller
    {
        //
        // GET: /TypeOfAuction/
        public ActionResult CheckLogIn_Require()
        {
            AgriMarketWebService service = new AgriMarketWebService();
            UserAccount userAccount = null;
            if (Session["CurrentUserAccount"] != null)
            {
                userAccount = service.GetUserAccountByUserName(Session["CurrentUserAccount"].ToString());
                ViewBag.CurrentUserAccount = userAccount;
                if (userAccount.TypeOfUser.TypeId != AdminCode.AdminTypeCode)
                    return Content("Bạn không có quyền truy cập!");
                return null;
            }
            else
            {
                TempData["ReturnUrl"] = Request.Url.PathAndQuery;
                return RedirectToAction("LogIn", "UserAccount");
            }
        }
        public void CheckLogIn_NoRequire()
        {
            AgriMarketWebService service = new AgriMarketWebService();
            UserAccount userAccount = null;
            if (Session["CurrentUserAccount"] != null)
            {
                userAccount = service.GetUserAccountByUserName(Session["CurrentUserAccount"].ToString());
                ViewBag.CurrentUserAccount = userAccount;
            }
        }
        public ActionResult Index()
        {
            AgriMarketWebService service = new AgriMarketWebService();
            ActionResult actionResult = CheckLogIn_Require();
            if (actionResult != null)
                return actionResult;
            var types = service.GetAllTypeOfAuction();
            return View(types);
        }

        //
        // GET: /TypeOfAuction/Details/5

        public ActionResult Details(int id)
        {
            
            return View();
        }

        //
        // GET: /TypeOfAuction/Create

        public ActionResult Create()
        {
            return View("AddTypeOfAuction");
        } 

        //
        // POST: /TypeOfAuction/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            AgriMarketWebService service = new AgriMarketWebService();
            ActionResult actionResult = CheckLogIn_Require();
            if (actionResult != null)
                return actionResult;
            try
            {
                // TODO: Add insert logic here
                string typeName = collection["typeName"];

                TypeOfAuction newType = new TypeOfAuction() { TypeName=typeName };
                int state = service.AddNewTypeOfAuction(newType);
                if (state == -1)
                    ModelState.AddModelError("Result","Lỗi thao tác thêm");
                var types = service.GetAllTypeOfAuction();
                return View("Index",types);
            }
            catch
            {
                ModelState.AddModelError("Result", "Lỗi thao tác thêm");
                var types = service.GetAllTypeOfAuction();
                return View("Index", types);
            }
        }
        
        //
        // GET: /TypeOfAuction/Edit/5
 
        public ActionResult Edit(int id)
        {
            AgriMarketWebService service = new AgriMarketWebService();
            ActionResult actionResult = CheckLogIn_Require();
            if (actionResult != null)
                return actionResult;
            var typeOfAuction = service.GetTypeOfAuctionById(id);
            if (typeOfAuction == null)
            {
                ModelState.AddModelError("TypeOfAuction", "Không tìm thấy kiểu đấu giá");
            }

            return View("EditTypeOfAuction", typeOfAuction);
        }

        //
        // POST: /TypeOfAuction/Edit/5

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            AgriMarketWebService service = new AgriMarketWebService();
            ActionResult actionResult = CheckLogIn_Require();
            if (actionResult != null)
                return actionResult;
            try
            {
                // TODO: Add update logic here
                int typeId=0;
                bool ok = true;
                if (!int.TryParse(collection["TypeId"], out typeId))
                {
                    ModelState.AddModelError("TypeId","Mã kiểu đấu giá không hợp lệ");
                    ok = false;
                }
                string typeName=collection["TypeName"];

                if (ok) {
                    TypeOfAuction updatedAuction = new TypeOfAuction()
                    {
                        TypeId=typeId,
                        TypeName=typeName
                    };
                    int state = service.UpdateTypeOfAuction(updatedAuction);
                    if (state == -1)
                    {
                        ModelState.AddModelError("Result", "Kiểu đấu giá không tồn tại");
                    }
                    else if (state == 1)
                    {
                        ModelState.AddModelError("Result", "Lỗi khi cập nhật");
                    } else  if(state==0)
                    {
                        return RedirectToAction("Index");
                    }
                }

                var typeOfAuction = service.GetTypeOfAuctionById(typeId);
                if (typeOfAuction == null)
                {
                    ModelState.AddModelError("TypeOfAuction", "Không tìm thấy kiểu đấu giá");
                }
                return View("EditTypeOfAuction", typeOfAuction);
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        //
        // GET: /TypeOfAuction/Delete/5
 
        public ActionResult Delete(int id)
        {
            AgriMarketWebService service = new AgriMarketWebService();
            ActionResult actionResult = CheckLogIn_Require();
            if (actionResult != null)
                return actionResult;
            int state = service.DeleteTypeOfAuctionById(id);
            if (state == -1)
            {
                ModelState.AddModelError("Result", "Kiểu đấu giá không tồn tại");
            }
            else if (state == 1)
            {
                ModelState.AddModelError("Result", "Lỗi khi xóa");
            }
            var types = service.GetAllTypeOfAuction();
            return View("Index",types);
        }

        //
        // POST: /TypeOfAuction/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
